/*
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
#ifndef APR_EVENT_H
#define APR_EVENT_H

#include "apr.h"
#include "apr_pools.h"
#include "apr_poll.h"
#include "apr_ring.h"
#include "apr_time.h"

typedef struct apr_event_loop_t  apr_event_loop_t;
typedef struct apr_event_timer_t apr_event_timer_t;
typedef struct apr_event_io_t    apr_event_io_t;
typedef enum   apr_event_state_t apr_event_state_t;

typedef void (*apr_event_timer_cb_t) (apr_event_timer_t *lp, void *baton);
typedef void (*apr_event_io_cb_t) (apr_event_loop_t *loop, apr_event_io_t *io);

#define APR_EVENT(io) (io->pfd)
#define APR_EVENT_RETURNED(io) (io->pfd->rtnevents)
#define APR_EVENT_TYPE(io) (io->pfd->desc_type)
#define APR_EVENT_SOCK(io) (io->pfd->desc.s)
#define APR_EVENT_FILE(io) (io->pfd->desc.f)

struct apr_event_timer_t
{
    APR_RING_ENTRY(apr_event_timer_t) link;
    apr_time_t expires;
    apr_event_timer_cb_t cb;
    void *baton;
    apr_pool_t *pool;
    apr_event_loop_t *loop;
};

enum apr_event_state_t
{
    APR_EVENT_LOOP_INIT,
    APR_EVENT_LOOP_RUNNING,
};

struct apr_event_io_t
{
    apr_pool_t *pool;
    apr_event_loop_t *loop;
    apr_event_io_cb_t func;
    apr_pollfd_t *pfd;
    void *baton;
};

struct apr_event_loop_t 
{
    int state;
    apr_pool_t *pool;
    apr_pollcb_t *pollcb;
    apr_thread_mutex_t *mtx;

    apr_event_io_cb_t io_cb;

    /* List of upcoming timers, sorted by nearest first. 
     */
    APR_RING_HEAD(apr_event_timer_ring_t, apr_event_timer_t) timer_ring;
};

/* APR IO Interface */
void apr_event_io_initsock(apr_event_io_t **io, 
                           apr_event_io_cb_t func,
                           apr_socket_t *sock, 
                           apr_int32_t flags, 
                           void *baton, 
                           apr_pool_t *pool);

apr_status_t apr_event_io_add(apr_event_loop_t *loop,
                      apr_event_io_t *io);

/* APR Timer */
void apr_event_register_timer(apr_event_loop_t *sc,
                              apr_event_timer_cb_t cb,
                              void *baton, 
                              apr_time_t relative_time,
                              apr_pool_t *shutdown_pool);

/* APR Event Loop */
/** 
 * @brief 
 * @param loop
 * @param flags
 * @param p
 * @return 
 */
apr_status_t apr_event_loop_create(apr_event_loop_t **loop, 
                                   apr_uint32_t flags, 
                                   apr_pool_t *p);

/** 
 * @brief 
 * @param loop
 * @return 
 */
int apr_event_loop(apr_event_loop_t *loop);

#endif
